package com.borun.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.borun.bean.User;
import com.borun.util.DBUtil;

public class UserDao {

	public static boolean Login(User user) throws SQLException {

		// 1.首先拿到数据库的连接
		Connection conn = DBUtil.getConnection();

		// 2.编写要执行的sql语句 参数用?表示，相当于占位符;
		String sql = "select userid from t_user where username=? and password = ?";
		// 3.预编译sql语句
		PreparedStatement psmt = conn.prepareStatement(sql);
		// 4.给SQL语句传递参数
		psmt.setString(1, user.getUsername());
		psmt.setString(2, user.getPassword());
		// 5.执行SQL语句 并接收返回值
		return psmt.execute();
	}

	public static boolean Regist(User user) throws SQLException {
		Connection conn = DBUtil.getConnection();
		String sql = "insert into t_user(username,password) values(?,?)";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, user.getUsername());
		psmt.setString(2, user.getPassword());
		return psmt.executeUpdate() > 0;
	}

}
